package com.koron.operation.startlog.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.DateAndTimeUtils;
import com.koron.common.core.util.StringUtils;
import com.koron.operation.OperationListener;
import com.koron.operation.dispatch.bean.vo.EqCockpitVo;
import com.koron.operation.dispatch.feign.MonitorFeign;
import com.koron.operation.startlog.bean.RealTimeBean;
import com.koron.operation.startlog.bean.StartLogBean;
import com.koron.operation.startlog.bean.StartLogChangeBean;
import com.koron.operation.startlog.bean.StartLogCheckBean;
import com.koron.operation.startlog.bean.bo.EqSortBean;
import com.koron.operation.startlog.bean.dto.*;
import com.koron.operation.startlog.bean.excelbean.StartLogTemplate;
import com.koron.operation.startlog.enums.StartLogConfirmEnum;
import com.koron.operation.startlog.mapper.StartLogBeanMapper;
import com.koron.operation.startlog.mapper.StartLogChangeBeanMapper;
import com.koron.operation.startlog.services.imp.StartLogServiceImp;
import com.koron.operation.util.ExportExcelUtil;
import com.koron.operation.util.StarLogUntil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.koron.operation.startlog.enums.StartLogEnum.*;

@Slf4j
@RequestMapping("/startlog")
@RestController
@Api(tags = "开停机日志")
public class StartLogController {

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @Autowired
    private MonitorFeign monitorfeign;

    @Autowired
    private StartLogServiceImp startlogserviceimp;


    @ApiOperation(value = "开停机操作提交")
    @GetMapping("/submit")
    public Response<String> submit(StartLogAddDto dto) {

        Assert.isFalse(StringUtils.isEmpty(dto.getOperateType()), "请选择开关机操作");

        StartLogBean oldbean = ADOConnection.runTask(getCurrDs(), factory -> {
            return factory.getMapper(StartLogBeanMapper.class).queryFirstByEquipmentId(dto.getEquipmentId());
        }, StartLogBean.class);

        if (ObjectUtil.isNotNull(oldbean)) {
            Assert.isFalse(oldbean.getIsConfirm().equals(StartLogConfirmEnum.Wair.getValue()), "请批复机组最新记录后再提交");
            StartLogCheckBean check = StartLogCheckBean.create(false, "");
            check = checkStartAdd(dto, oldbean, check);//检查参数规范
            Assert.isFalse(check.getFalg(), check.getDescription());
        }

        StartLogBean bean = Convert.convert(StartLogBean.class, dto);
        bean.setIsConfirm(1);
        ADOConnection.runTask(getCurrDs(), startlogserviceimp, "saveStartLog", void.class, bean);
        return Response.success("成功");
    }

    @ApiOperation(value = "开停机操作列表")
    @GetMapping("/queryLogList")
    public Response<List<StartOperDto>> queryLogList() {
        List<StartOperDto> list = ADOConnection.runTask(getCurrDs(), startlogserviceimp, "queryList", List.class);
        return Response.ok(list);
    }

    @ApiOperation(value = "公司驾驶舱-设备运行状态")
    @GetMapping("/eqCockpitVo")
    public Response<List<EqCockpitVo>> eqCockpitVo() {
        if (true) {
            return Response.ok();
        }
        List<StartOperDto> list = ADOConnection.runTask(getCurrDs(), startlogserviceimp, "cockpitVoqueryList", List.class);
        if (CollUtil.isEmpty(list)) {
            return Response.ok(new ArrayList<>());
        }
        Map<String, String> deptManageNameIdmap = list.stream().collect(Collectors.toMap(StartOperDto::getDeptManageName, StartOperDto::getDeptManage, (d1, d2) -> d1));//map(单位名称，单位id)
        // 管理部门-对应台账id
        Map<String, String> deptManageNameEqId = list.stream().filter(d -> d.getDeptManageEquipmentId() != null).collect(Collectors.toMap(StartOperDto::getDeptManageName, StartOperDto::getDeptManageEquipmentId, (d1, d2) -> d1));//map(单位名称，泵张台账id)
        Map<String, List<StartOperDto>> collect = list.stream().collect(Collectors.groupingBy(StartOperDto::getDeptManageName));//map(单位名称， 单位下机组开关最新状态)

        List<EqCockpitVo> resultlist = new ArrayList();
        // 获取台账排序信息
        List<EqSortBean> eqsortlist = getSort("eq");
        Map<String, Integer> eqsortmap = eqsortlist.stream().collect(Collectors.toMap(EqSortBean::getEquipmentId, EqSortBean::getSort));//获取台账
        for (Map.Entry<String, List<StartOperDto>> entry : collect.entrySet()) {
            List<StartOperDto> dblist = entry.getValue();
            EqCockpitVo eqCockpitVo = new EqCockpitVo();
            eqCockpitVo.setDeptManage(deptManageNameIdmap.get(entry.getKey()));//单位id
            String eqid = deptManageNameEqId.get(entry.getKey());
            eqCockpitVo.setDeptManageEquipmentId(eqid);//泵站台账id
            eqCockpitVo.setName(entry.getKey());//单位名称
            eqCockpitVo.setSort(eqsortmap.get(eqid));//排序
            if (CollUtil.isNotEmpty(dblist)) {
                long count = dblist.stream().filter(d -> d.getOperateType() != null && d.getOperateType().equals(START_UP.getStr())).count();
                eqCockpitVo.setStartQty(count);
            } else {
                eqCockpitVo.setStartQty(0l);
            }
            resultlist.add(eqCockpitVo);
        }

        // ==============================================================================================
        //实时抽水量
        for (EqCockpitVo eqCockpitVo : resultlist) {
            // 没有找到相应的泵站id
            if (StrUtil.isBlank(eqCockpitVo.getDeptManageEquipmentId())) {
                eqCockpitVo.setActualFlow(BigDecimal.ZERO);
                continue;
            }
            Response<List<RealTimeBean>> response = monitorfeign.getRealTime("yggs", eqCockpitVo.getDeptManageEquipmentId(), null);
            if (response.getCode() != 200) {
                eqCockpitVo.setActualFlow(BigDecimal.ZERO);
                continue;
            }
            List<RealTimeBean> responselist = response.getData();
            Optional<RealTimeBean> actualFlowOpt = responselist.stream().filter(e -> StrUtil.contains(e.getParamName(), "出口流量实际值")).findFirst();
            if (actualFlowOpt.isPresent()) {
                String paramValue = actualFlowOpt.get().getParamValue();
                BigDecimal bigde = new BigDecimal(paramValue).setScale(1, BigDecimal.ROUND_DOWN);
                eqCockpitVo.setActualFlow(bigde);
            } else {
                eqCockpitVo.setActualFlow(BigDecimal.ZERO);
            }
        }

        return Response.ok(resultlist);
    }


    /**
     * 获取台账排序
     *
     * @return
     */
    public List<EqSortBean> getSort(String eqType) {
        List<EqSortBean> list = ADOConnection.runTask(getCurrDs(), factory -> {
            return factory.getMapper(StartLogBeanMapper.class).getSort(eqType).stream()
                    .filter(es->es.getEquipmentId()!=null && es.getSort()!=null).collect(Collectors.toList());
        }, List.class);
        return list;
    }

    /**
     * 获取台账排序
     *
     * @return
     */
    public List<EqSortBean> getOrgSort(String eqType) {
        List<EqSortBean> list = ADOConnection.runTask(getCurrDs(), factory -> {
            return factory.getMapper(StartLogBeanMapper.class).getSort(eqType).stream()
                    .filter(es-> es.getSort()!=null ).collect(Collectors.toList());
        }, List.class);
        return list;
    }

    @GetMapping("/orgSort")
    @ApiOperation("公司驾驶舱-组织排序")
    public Response otherCockpitTicket(@RequestParam("year") String year) {
        return Response.success(getOrgSort("org"));
    }


    @ApiOperation(value = "开停机日志列表-分页")
    @GetMapping("/queryOldStatusList")
    public Response<PageInfo> queryOldStatusList(StartLogDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize(), dto.getOrderBy());
        List<StartLogBean> list = ADOConnection.runTask(getCurrDs(), startlogserviceimp, "querylogList", List.class, dto);
        return Response.ok(new PageInfo(list));
    }

    @ApiOperation(value = "导出")
    @GetMapping("/logExport")
    public void logExport(HttpServletResponse rsp, StartLogDto dto) {
        PageHelper.startPage(1, 9999, dto.getOrderBy());
        List<StartLogBean> dblist = ADOConnection.runTask(getCurrDs(), startlogserviceimp, "querylogList", List.class, dto);
        List<StartLogTemplate> explist = new ArrayList<>();
        try {
            if (StringUtils.isNotEmpty(dblist)) {
                Map<String, String> dicmap = CodeTools.getDictByClassCode("operationManagement", "dispatch_operate_type");//字典对应的key value
                dblist.forEach(b -> {
                    b.setOperateType(dicmap.get(b.getOperateType()));//操作类型 字典翻译
                    explist.add(Convert.convert(StartLogTemplate.class, b));
                });
                explist.stream().forEach(e -> e.setIsConfirmStr(StarLogUntil.convenIsConfirm(e.getIsConfirm(), e.getIsTest())));//转换确认状态
                ExportExcelUtil.exportExcel(explist, StartLogTemplate.class, "开停机日志历史", rsp);
            }
        } catch (Exception e) {
            log.error("导出失败");
            log.error(e.getMessage());
        }

        return;
    }

    @ApiOperation(value = "开停机日志详情")
    @GetMapping("/logDetails")
    public Response<StartLogBean> logDetails(String id) {
        StartLogBean bean = ADOConnection.runTask(getCurrDs(), startlogserviceimp, "logDetails", StartLogBean.class, id);
        bean.setDeptId(bean.getDeptManage());//复制
        bean.setDeptName(bean.getDeptManageName());//复制
        return Response.ok(bean);
    }

    @Deprecated
    @ApiOperation(value = "开停机日志修改 废弃")
    @GetMapping("/update")
    public Response<String> update(StartLogDetailDto dto) {

        Boolean flag = ADOConnection.runTask(getCurrDs(), factory -> {
            StartLogBeanMapper mapper = factory.getMapper(StartLogBeanMapper.class);
            StartLogBean oldbean = mapper.selectByPrimaryKey(dto.getId());
            Date maxtime;
            Date mintime;
            List<StartLogBean> dlist = mapper.queryAllByOperateTimeAfterAndEquipmentId(oldbean.getOperateTime(), oldbean.getEquipmentId(), dto.getId());
            List<StartLogBean> xlist = mapper.queryAllByOperateTimeBeforeAndEquipmentId(oldbean.getOperateTime(), oldbean.getEquipmentId(), dto.getId());

            if (dlist.size() >= 2) {
                maxtime = dlist.get(1).getOperateTime();
            } else {
                maxtime = DateAndTimeUtils.strtoDate("9999-12-30", "yyyy-MM-dd");
            }
            if (xlist.size() >= 2) {
                mintime = xlist.get(1).getOperateTime();
            } else {
                mintime = DateAndTimeUtils.strtoDate("1970-01-01", "yyyy-MM-dd");
            }
            //不可以等于上一条记录 或 下一条记录的 操作时间
            if (DateUtil.compare(dto.getOperateTime(), maxtime) == 0 || DateUtil.compare(dto.getOperateTime(), mintime) == 0) {
                return false;
            }
            //必须在 上一条记录 或 下一条记录的 操作时间 范围内
            return DateUtil.isIn(dto.getOperateTime(), maxtime, mintime);
        }, Boolean.class);

        Assert.isTrue(flag, "操作时间不可以大于后一条记录 或 小于前一条记录");

        StartLogBean bean = Convert.convert(StartLogBean.class, dto);
        String id = ADOConnection.runTask(getCurrDs(), startlogserviceimp, "update", String.class, bean);
        return Response.ok(id);
    }

    @ApiOperation(value = "开停机日志修改new")
    @GetMapping("/updateNew")
    public Response<String> updateNew(StartLogDetailDto dto) {

        checkReasonable(dto);//检查修改日志合理性

        StartLogBean bean = Convert.convert(StartLogBean.class, dto);
        String id = ADOConnection.runTask(getCurrDs(), startlogserviceimp, "update", String.class, bean);
        return Response.ok(id);
    }


    @ApiOperation(value = "作废")
    @GetMapping("/tovoid")
    public Response<String> tovoid(@RequestParam("id") String id) {
        Boolean aBoolean = ADOConnection.runTask(getCurrDs(), factory -> {
            StartLogBeanMapper mapper = factory.getMapper(StartLogBeanMapper.class);
            StartLogBean bean = mapper.selectByPrimaryKey(id);
            StartLogBean topOne = mapper.queryFirstByEquipmentIdFQ(bean.getEquipmentId());
            Assert.isFalse(!topOne.getId().equals(id) && !topOne.getOperateType().equals("START_FAIL"), "该记录不是设备最新记录不允许作废");
            mapper.updateIsConfirmById(StartLogConfirmEnum.Discard.getValue(), id);
            return true;
        }, Boolean.class);
        return Response.ok(id);
    }

    /**
     * 检查修改日志合理性
     *
     * @param dto
     */
    private Boolean checkReasonable(StartLogDetailDto dto) {
        int nowcompare = DateUtil.compare(new Date(), dto.getOperateTime());
        Assert.isFalse(nowcompare < 0, "操作时间，不可以填写未来时间");
        return ADOConnection.runTask(getCurrDs(), factory -> {
            StartLogBeanMapper mapper = factory.getMapper(StartLogBeanMapper.class);
//            StartLogBean oldbean = mapper.selectByPrimaryKey(dto.getId());
            Date maxtime = new Date();
            Date mintime = new Date();
            List<StartLogBean> dlist = mapper.queryAllByOperateTimeAfterAndEquipmentId(dto.getOperateTime(), dto.getEquipmentId(), dto.getId());
            List<StartLogBean> xlist = mapper.queryAllByOperateTimeBeforeAndEquipmentId(dto.getOperateTime(), dto.getEquipmentId(), dto.getId());
            StartLogCheckBean check = null;
            if (CollUtil.isNotEmpty(dlist)) {
                StartLogBean afterBean = dlist.get(0);//取个该修改使时间的后一条记录
                maxtime = afterBean.getOperateTime();
                Assert.isFalse(DateUtil.compare(maxtime, dto.getOperateTime()) == 0, "不能与后一条记录操作时间相同");
                check = StarLogUntil.checkOpt(dto.getOperateType(), null, afterBean.getOperateType());
                Assert.isFalse(!check.getFalg(), check.getDescription());
            }
            if (CollUtil.isNotEmpty(xlist)) {
                StartLogBean berforBean = xlist.get(0);//取个该修改使时间的前一条记录
                mintime = berforBean.getOperateTime();
                Assert.isFalse(DateUtil.compare(mintime, dto.getOperateTime()) == 0, "不能与上一条记录操作时间相同");
                check = StarLogUntil.checkOpt(dto.getOperateType(), berforBean.getOperateType(), null);
                Assert.isFalse(!check.getFalg(), check.getDescription());
            }
            if (DateUtil.compare(dto.getOperateTime(), maxtime) == 0 || DateUtil.compare(dto.getOperateTime(), mintime) == 0) {
                Assert.isFalse(true, "操作时间不可以等于 前一条记录 或 后一条记录");
            }
            return true;
        }, Boolean.class);
    }


    /**
     * 检查开机操作参数规范
     *
     * @param dto
     * @param oldbean
     * @param check
     * @return
     */
    private StartLogCheckBean checkStartAdd(StartLogAddDto dto, StartLogBean oldbean, StartLogCheckBean check) {
        int nowcompare = DateUtil.compare(new Date(), dto.getOperateTime());
        Assert.isFalse(nowcompare < 0, "操作时间，不可以填写未来时间");
        Assert.isFalse(dto.getOperateType().equals(START_FAIL.getStr()) && StringUtils.isBlank(dto.getContent()), "开机不成功操作必须填写备注");
        Assert.isFalse(dto.getOperateType().equals(START_FAIL.getStr()) && dto.getInPlan() == 0, "开机成功或开机不成功一定是计划内");
        Assert.isFalse(dto.getOperateType().equals(START_UP.getStr()) && dto.getInPlan() == 0, "开机成功或开机不成功一定是计划内");

        ArrayList<String> accidentlist = CollectionUtil.newArrayList(ACCIDENT.getStr(), BREAKDOWN.getStr());//意外状态
        ArrayList<String> downlist = CollectionUtil.newArrayList(ACCIDENT.getStr(), BREAKDOWN.getStr(), SHUT_DOWN.getStr(), START_FAIL.getStr());
        if (accidentlist.contains(dto.getOperateType()) && dto.getInPlan() == 1) {
            check = StartLogCheckBean.create(true, "该操作状态属于计划外");
        }

        if (StringUtils.isNotNull(oldbean)) {
            int oldcompare = DateUtil.compare(oldbean.getOperateTime(), dto.getOperateTime());
            Assert.isFalse(oldcompare >= 0, "操作时间，不能小于等于上次操作时间");


            if (oldbean.getOperateType().equals(dto.getOperateType())) {
                check = StartLogCheckBean.create(true, "操作状态与当前设备状态不能相同");
            }

            //开机状态下判断
            if (oldbean.getOperateType().equals(START_UP.getStr())) {
                if (dto.getOperateType().equals(START_FAIL.getStr()) || dto.getOperateType().equals(START_UP.getStr())) {
                    check = StartLogCheckBean.create(true, "该设备已开机，无需再次开机");
                }
            }

            //停机状态下判断
            if (downlist.contains(oldbean.getOperateType()) && !dto.getOperateType().equals(START_FAIL.getStr())) {
                if (downlist.contains(dto.getOperateType())) {
                    check = StartLogCheckBean.create(true, "开机状态下才能停机");
                }
            }
        }
        return check;
    }


    @ApiOperation(value = "开停机日志-批量 删除")
    @PostMapping("/logDelete")
    public Response<Integer> logDelete(@RequestBody List<String> list) {
        Assert.isFalse(CollUtil.isEmpty(list), "标识不能为空");
        Integer flag = ADOConnection.runTask(getCurrDs(), factory -> {
            int i = factory.getMapper(StartLogBeanMapper.class).deleteInPrimaryKey(list);
            return i;
        }, Integer.class);
        return Response.ok(flag);
    }

    @ApiOperation(value = "是否确认   是否1确认。默认0未确认 2待批复")
    @PostMapping("/confirm")
    public Response<String> confirm(@RequestBody StartLogIsConfirmDto dto) {
        Assert.isFalse(CollUtil.isEmpty(dto.getList()), "标识不能为空");
        ADOConnection.runTask(getCurrDs(), factory -> {
            StartLogBeanMapper mapper = factory.getMapper(StartLogBeanMapper.class);
            List<StartLogBean> checklist = mapper.queryAllInIdAndOperateType(dto.getList(), DOWN_FAIL.getStr());
            Assert.isFalse(CollUtil.isNotEmpty(checklist), "确认的数据不能含有停机不成功的记录");
            int i = mapper.updateIsConfirmByIdList(dto.getIsConfirm(), dto.getList());
            return i;
        }, Integer.class);
        return Response.ok("成功");
    }

    @ApiOperation(value = "是否试机 true是 false不是")
    @PostMapping("/istest")
    public Response<String> istest(@RequestBody StartLogIsConfirmDto dto) {
        Assert.isFalse(CollUtil.isEmpty(dto.getList()), "标识不能为空");
        ADOConnection.runTask(getCurrDs(), factory -> {
            int i = factory.getMapper(StartLogBeanMapper.class).updateIsTestById(dto.getIsTest(), dto.getList());
            return i;
        }, Integer.class);
        return Response.ok("成功");
    }


    @ApiOperation(value = "数据变更")
    @PostMapping("/change")
    public Response<String> change(@RequestBody StartLogChangeBean bean) {
        Assert.isFalse(StrUtil.isBlank(bean.getSourceId()), "基本信息标识不能为空");
        StartLogDetailDto dto = Convert.convert(StartLogDetailDto.class, bean);
        dto.setId(bean.getSourceId());
        checkReasonable(dto);//检查修改日志合理性
        bean.setAppPerson(getCurrUser().getAccount());
        bean.setAppPersonName(getCurrUser().getName());
        bean.setAppTime(new Date());
        String id = ADOConnection.runTask(getCurrDs(), factory -> {
            StartLogChangeBeanMapper mapper = factory.getMapper(StartLogChangeBeanMapper.class);
            List<StartLogChangeBean> changeDet = mapper.queryAllBySourceIdWait(bean.getSourceId());//查询是否存在待批复的变更
            Assert.isFalse(CollUtil.isNotEmpty(changeDet), "存在待批复的变更，不允许再提交申请");

            //变更原日志的状态 为 待批复
            factory.getMapper(StartLogBeanMapper.class).updateIsConfirmById(StartLogConfirmEnum.Wair.getValue(), bean.getSourceId());
            //新增变更数据
            mapper.insertSelective(bean);

            return bean.getId();
        }, String.class);
        return Response.ok(id);
    }

    @ApiOperation(value = "是否通过  注意返回的是基本信息的id")
    @PostMapping("/adopt")
    public Response<String> adopt(@RequestParam("id") String id, @RequestParam("isadopt") Boolean isadopt, @RequestParam(value = "opinion", required = false) String opinion) {

        String baseId = ADOConnection.runTask(getCurrDs(), factory -> {
            StartLogChangeBeanMapper mapper = factory.getMapper(StartLogChangeBeanMapper.class);
            StartLogChangeBean changeBean = mapper.selectByPrimaryKey(id);//变更信息
            changeBean.setReplyTime(new Date());
            changeBean.setReplyPerson(getCurrUser().getAccount());//批复人帐号
            changeBean.setReplyPersonName(getCurrUser().getName());//批复人名称
            changeBean.setIsadopt(isadopt);
            changeBean.setOpinion(opinion);
            mapper.updateByPrimaryKey(changeBean);

            StartLogBeanMapper startlogbeanmapper = factory.getMapper(StartLogBeanMapper.class);
            //如果不通过
            if (!isadopt) {
                startlogbeanmapper.updateIsConfirmByIdList(1, CollUtil.newArrayList(changeBean.getSourceId()));
                return changeBean.getSourceId();
            }
            StartLogBean convert = Convert.convert(StartLogBean.class, changeBean);//变更信息转换成基本信息
            //如果不是试机的 审核通过 变更原日志的的状态
            if (!convert.getIsTest()) {
                convert.setIsConfirm(StartLogConfirmEnum.Hit.getValue());
            }
            convert.setId(changeBean.getSourceId());
            convert.setIsConfirm(1);//确认
            startlogbeanmapper.updateByPrimaryKeySelective(convert);
            return changeBean.getSourceId();
        }, String.class);
        return Response.ok(baseId);
    }


    @ApiOperation(value = "变更信息详情")
    @GetMapping("/changeMsgdetails")
    public Response<StartLogChangeBean> changeMsgdetails(@RequestParam("id") String id) {
        StartLogChangeBean changeBean = ADOConnection.runTask(getCurrDs(), factory -> {
            return factory.getMapper(StartLogChangeBeanMapper.class).selectByPrimaryKey(id);
        }, StartLogChangeBean.class);
        return Response.ok(changeBean);
    }

    @Autowired
    OperationListener operationListener;

    @ApiOperation(value = "监控数据同步")
    @GetMapping("/testrwzq")
    public Response testrwzq() {
        operationListener.synStartLog();
        return Response.ok();
    }

    @ApiOperation(value = "驾驶仓-开机成功率")
    @GetMapping("/cockpitUnitStartSuccessRatio")
    public Response cockpitUnitStartSuccessRatio() {
        Integer ratio = ADOConnection.runTask(getCurrDs(), factory -> {
            return factory.getMapper(StartLogBeanMapper.class).ratio();
        }, Integer.class);
        return Response.ok(ratio);
    }

}
